<?php

namespace App\Http\Model;

/**
 * 足迹（我看过谁）
 */
class FriendBrowses extends Base
{
    /**
     * 表名字
     *
     * @var string
     */
    protected $table = 'friends_browse';

    /**
     * 指示模型是否自动维护时间戳
     *
     * @var bool
     */
    public $timestamps = false;

    /**
     * 模型日期列的存储格式。
     *
     * @var string
     */
    protected $dateFormat = 'U';

    /**
     * 可以被批量赋值的属性。
     *
     * @var array
     */
    protected $fillable = ['id', 'user_id', 'browse_user_id', 'create_time', 'create_timestamp'];

    /**
     * 构造函数
     * @param array $attributes
     */
    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
    }

    /**
     * 查询用户近 $day 天的足迹统计数据
     * @param int $userId
     * @param int $day
     * @return array
     */
    public function getUserFootprintStatisticsData(int $userId, int $day): array
    {
        $time = time() - $day * 24 * 3600;//计算时间
        return FriendBrowses::query()->selectRaw('browse_user_id as user_id')->selectRaw('count(*) as number')
            ->selectRaw('max(create_timestamp) as create_time')->where('user_id', $userId)
            ->where('create_timestamp', '>=', $time)->groupBy(['browse_user_id'])->orderByDesc('create_time')
            ->get()->toArray();
    }

    /**
     * 格式化用户统计数据
     *
     * @param array $list
     * @return array
     */
    public function formatUserStatisticsData(array $list): array
    {
        $result = [];
        $userIds = array_column($list, 'user_id');
        $users = collect((new User())->get_users_by_ids($userIds))->keyBy('user_id')->toArray();
        foreach ($list as $index => &$item) {
            $userId = $item['user_id'];
            $userInfo = $users[$userId] ?? [];
            if (!$userInfo) {
                unset($list[$index]);
                continue;
            }

            $item['user_uid'] = $userInfo['user_uid'] ?? '';//用户的uuid
            $item['nickname'] = $userInfo['nickname'] ?? '';//昵称
            $item['image'] = $userInfo['images'][0] ?? '';//头像
            $item['gender'] = $userInfo['gender'] ?? 3;//性别 性别 1:男  2：女 3:未知
            $item['view_time'] = getViewTimeMsg($item['create_time']);//时间
            unset($item['create_time']);
            $result[] = $item;
        }

        return $result;
    }

    /**
     * 查询用户近 $day 天的访客统计数据
     * @param int $userId
     * @param int $day
     * @return array
     */
    public function getUserVisitorsStatisticsData(int $userId, int $day): array
    {
        $time = time() - $day * 24 * 3600;//计算时间
        return FriendBrowses::query()->select(['user_id'])->selectRaw('count(*) as number')
            ->selectRaw('max(create_timestamp) as create_time')->where('browse_user_id', $userId)
            ->where('create_timestamp', '>=', $time)->groupBy(['user_id'])->orderByDesc('create_time')
            ->get()->toArray();
    }
}